package com.jian.sys.common;

import com.jian.bus.mapper.GoodsMapper;
import com.jian.bus.mapper.ProviderMapper;
import com.jian.bus.pojo.Goods;
import com.jian.bus.pojo.Provider;
import com.jian.sys.mapper.DeptMapper;
import com.jian.sys.mapper.UserMapper;
import com.jian.sys.pojo.Dept;
import com.jian.sys.pojo.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.swing.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 缓存池
 */
@Data
public class CachePool {
    public static volatile Map<String,Object> CACHE_CONTAINER=new HashMap<>();

    /**
     *  通过key删除缓存
     * @param key
     */
    public static void removeCacheByKey(String key){
        CACHE_CONTAINER.remove(key);
    }

    /**
     * 清空所有的缓存
     */
    public static void removeAll(){
        CACHE_CONTAINER.clear();
    }
    /**
     * 同步缓存
     */
    public static void syncCache(){
        //同步商品管理缓存
        GoodsMapper goodsMapper = SpringUtils.getBean(GoodsMapper.class);
        List<Goods> goodsList = goodsMapper.selectList(null);
        for (Goods goods : goodsList) {
            CACHE_CONTAINER.put("goods:"+goods.getId(),goods);
        }
        //同步供应商管理缓存
        ProviderMapper providerMapper = SpringUtils.getBean(ProviderMapper.class);
        List<Provider> providerList = providerMapper.selectList(null);
        for (Provider provider : providerList) {
            CACHE_CONTAINER.put("provider:"+provider.getId(),provider);
        }
        //同步部门管理缓存
        DeptMapper deptMapper = SpringUtils.getBean(DeptMapper.class);
        List<Dept> deptList = deptMapper.selectList(null);
        for (Dept dept : deptList) {
            CACHE_CONTAINER.put("dept:"+dept.getId(),dept);
        }
        //同步用户管理缓存
        UserMapper userMapper = SpringUtils.getBean(UserMapper.class);
        List<User> userList = userMapper.selectList(null);
        for (User user : userList) {
            CACHE_CONTAINER.put("user:"+user.getId(),user);
        }
    }
}
